(function() {
  app.controller('homeCtrl', function(banner_config, $scope, $rootScope, tradeApi, $filter, $interval, header_m, alertTip, $state, local_m) {
    header_m.set({
      css_l: {
        name: 'icon-user'
      },
      fun_l: function() {
        local_m
          .set('my_index_back', 'home');
        $state.go('my_index');
      },
      css_r: {
        name: 'icon-phone'
      },
      fun_r: function() {
        $state.go('novice');
      }
    });

    $(document).ready(function() { // 轮播图配置
      new Swiper('.swiper-container', banner_config.config);
    });

    $scope.banners = banner_config.imgs;
    $scope.home_list_actived = 'report';
    $scope.init = function() {
      tradeApi.query({
        funcId: '000004',
        data: {
          oper_type: '1'
        }
      }).then(function(res) {
        angular.forEach(res, function(item) {
          item.sort_person_rate = parseFloat(item.person_rate);
        });
        $scope.home_list_report = res;
      });
      $scope.reload();
    };

    $scope.yiyuan_click = function() {
      local_m.set('type_yiyuan', 'yiyuan');
      $state.go('yiyuan');
    };

    $scope.reload = function() {
      tradeApi.query({
        funcId: '000004',
        data: {
          oper_type: '0'
        }
      }).then(function(res) {
        $scope.home_list_create = res;
      });
    };

    $scope.go_more = function() {
      if ($scope.home_list_actived === 'report') {
        $state.go('home_more', { type: 'report' });
      }
      if ($scope.home_list_actived === 'create') {
        $state.go('home_more', { type: 'create' });
      }
    };
    $scope.go_strategy = function(item) {
      item.stkinfo = {
        code: item.product_code,
        name: item.product_name,
        market: item.market
      };
      $scope.storage.local('detail_codeinfo', item.stkinfo);
      local_m.set('selected_strategy_options', item);
      $state.go('strategy');
    };

    $scope.go_detail = function(item) {
      item.income_balance = item.allot_balance;
      item.close_status = '2';
      $scope.storage.local('report_dl', item);
      $state.go('details-report', { source: true });
    };
    var timer = $interval(function() {
      $scope.reload();
    }, 5000);

    $scope.$on('$destroy', function() {
      $interval.cancel(timer);
      timer = null;
    });

  });

  app.filter('time_interval', function() {
    return function(input) {
      if (!input) return input;
      if (input < 1) {
        return '刚刚';
      }
      if (input >= 1 && input <= 59) {
        return input + '分钟前';
      }
      if (input > 59 && input < 24 * 60) {
        return Math.floor(input / 60) + '小时前';
      }
      if (input >= 24 * 60) {
        return Math.floor(input / 60 / 24) + '天前';
      }
    };
  });
})();